<?php

class QShippingtime
{
    /**
     *
     * @var Common_Db
     */
    private $_db;

    public function __construct()
    {
        $appUI = Zend_Registry::get('AppUI');
        $this->_db = new Common_Db('shippingtime_' . $appUI->country);
        $this->_db->query ( "SET NAMES 'utf8'" );
		$this->_db->query ( "SET CHARACTER SET 'utf8'" );
    }

    public function getRules($data_type = 'rule_pending', $filters = null)
    {
    	$db = '';
    	if($data_type == 'rule_active')
    		$db = 'rule_compile';
    	else
    		$db = 'rule';
    	
        $strQuery = "SELECT * FROM {$db} WHERE 1";
        
        // WHERE
        $strWhere = '';
        if($filters)
        {
        	foreach ($filters as $key => $value)
        		$strWhere .= " AND `$key`=$value";
        }
        $strQuery .= $strWhere;
        
        // ORDER
        $strQuery .= " ORDER BY `order`";        
        $arrResult = $this->_db->fetchAll($strQuery);
       
        $this->_db->closeConnection();
        return $arrResult;
    }

    public function updateRule($arrParams, $rule_id)
    {
        // update order
    	if(isset($arrParams['order']) && !empty($arrParams['order']))
    	{
    		$order = $arrParams['order'];    		
    		$rulesByOrder = $this->getRules(array('order' => $order));
    		if(!empty($rulesByOrder) && $rulesByOrder[0]['id'] != $rule_id)
    		{
    			$strQuery = "UPDATE rule SET `order`= `order` +1 WHERE `order` >={$order}";
    			$this->_db->query($strQuery);
    		}	    		
    	}
        
        $arrResult = $this->_db->update('rule', $arrParams, "id=$rule_id");
        $this->_db->closeConnection();
        return $arrResult;
    }
    
    public function deleteRule($rule_id)
    {
        $arrResult = $this->_db->delete('rule', "id=$rule_id");
        $this->_db->closeConnection();
        return $arrResult;
    }
        
        // public function import($table, $file)
        // {
        // $sql = "TRUNCATE TABLE {$table};";
        // $this->_db->query($sql);
        
        // }
      
public function copyData($fromTable, $toTable)
    {
    	$sql = "INSERT INTO `{$toTable}`
				SELECT *
				FROM `{$fromTable}` ORDER BY `order`;";
    	
    	 $this->_db->query($sql);    	
    }
	
    public function truncate ($table)
    {
        $sql = "TRUNCATE TABLE {$table};";
        $this->_db->query($sql);
    }

    public function insertLocation($arrParams)
    {
    	$this->_db->insert('location', $arrParams);
    }
    
    public function getMaxOrderRule()
    {
    	$strQuery = 'SELECT MAX(`order`) FROM `rule`';
    	return $this->_db->fetchOne($strQuery);    	
    } 
    
    public function getMaxIdRule()
    {
    	$strQuery = 'SELECT MAX(`id`) FROM `rule`';
    	return $this->_db->fetchOne($strQuery);    	
    } 
	
	public function getLastInsertId($tableName = null, $primaryKey = null)
	{
		return $this->_db->lastInsertId($tableName = null, $primaryKey = null);
	}
    
    public function insertRule($arrParams)
    {
    	// update order
    	if(isset($arrParams['order']) && !empty($arrParams['order']))
    	{
    		$order = $arrParams['order'];    		
    		$rulesByOrder = $this->getRules(array('order' => $order));
    		if(!empty($rulesByOrder))
    		{
    			$strQuery = "UPDATE rule SET `order`= `order` +1 WHERE `order` >={$order}";
    			$this->_db->query($strQuery);
    		}	    		
    	}
    	return $this->_db->insert('rule', $arrParams);
    }
    
    public function getLocations()
    {
        $strQuery = 'SELECT * FROM location';
        $arrResult = $this->_db->fetchAll($strQuery);
        $this->_db->closeConnection();
        return $arrResult;
    }
}
?>